package agents;

import game.constants.ReferenceAgent;
import game.constants.ReferenceSpecialFeature;
import interfaces.impl.Agent;
import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import actions.ActionXYtoMove;
import constants.Constants;
import extra.Position;

public class AgentFollow extends Agent{
	ActionXYtoMove actionXYtoMove;
	
	SpetialFeature targetToFollow;
	SpetialFeature visionRange;
	
	double xPrev;
	double yPrev;
	
	WorldObject objectToFollow = null;
	
	private double contTime = 0;

	@Override
	public void initializeActionList() {
		actionXYtoMove= new ActionXYtoMove();
		listActions.add(actionXYtoMove);
		
	}

	@Override
	public void initializeSpecialFeatures() {
		targetToFollow= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_FOLLOW);
		listFeatures.add(targetToFollow);
		
		visionRange = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_VISION_RANGE);
		listFeatures.add(visionRange);
		
		
	}

	@Override
	public void initializeName() {
		name="Agent Follow";
		
	}

	@Override
	public void initializeId() {
		this.id = ReferenceAgent.AGENT_FOLLOW;
		
	}

	@Override
	public void updateChild(WorldObject object, Universe universe, double time) {
		
		if(targetToFollow.getValue()==Constants.NO_OBJECT_ID){
			return;
		}
		
		contTime=contTime+time;
		if(contTime<200){
			return;
		}
		contTime=0;
		objectToFollow = universe.getObject(((int)targetToFollow.getValue()));
		if(objectToFollow==null){
			return;
		}
		
		
		double distance = Position.DistanceBetween(objectToFollow.getPosition(),object.getPosition());
		if(distance>visionRange.getValue() || distance<object.getImageObject().getImageToShow().getHeight() 
				|| distance<object.getImageObject().getImageToShow().getWidth()){
			
			System.out.println("NO LO VEO");
			return;
		}
		
		System.out.println("Lo Vi");
		
		SpetialFeature attackRange = object.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_ATTACK_RANGE);
		if(attackRange!=null){
			if(distance<attackRange.getValue()){
	
				actionXYtoMove.setPosition(object.getPosition());	
			
			}
			else{
				actionXYtoMove.setPosition(objectToFollow.getImageObject().getPosition());	
			}
			
			actionXYtoMove.setSource(object);
			actionXYtoMove.setTarget(object);
			universe.setAction(actionXYtoMove);
			return;
		}
		
		actionXYtoMove.setSource(object);
		actionXYtoMove.setTarget(object);
		actionXYtoMove.setPosition(objectToFollow.getImageObject().getPosition());
		universe.setAction(actionXYtoMove);
		
	}
	
	
	

}
